name: Publish package to the Maven Central Repository
on:
  release:
    types: [created]
jobs:
  publish:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install GPG secret key
        run: |
          # Install gpg secret key
          cat <(echo -e "${{ secrets.MAVEN_GPG_PRIVATE_KEY }}") | gpg --batch --import
          gpg --list-secret-keys
      - name: Set Version
        run: |
          echo "Tag name: $GITHUB_REF_NAME"
          mvn versions:set -DnewVersion=$GITHUB_REF_NAME
      - name: Set up Maven Central Repository
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'adopt'
          server-id: ossrh
          server-username: MAVEN_USERNAME
          server-password: MAVEN_PASSWORD
          gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }}
          gpg-passphrase: MAVEN_GPG_PASSPHRASE
      - name: Publish package
        run: |
          mvn \
          --no-transfer-progress \
          --batch-mode \
          -Dgpg.passphrase=${{ secrets.MAVEN_GPG_PASSPHRASE }} \
          -Dgpg.keyname=C9AAFE214609D9C5 \
          verify gpg:sign install:install deploy:deploy
        env:
          MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
          MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
          MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}